//
//  DwonLoadManager.m
//  基于NSURLSession的断点下载队列
//
//  Created by 邹浩 on 15/9/17.
//  Copyright (c) 2015年 蓝鸥. All rights reserved.
//

#import "DwonLoadManager.h"
#import "DwonLoad.h"
static DwonLoadManager *manager;
@implementation DwonLoadManager



+(instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[DwonLoadManager alloc] init];
            manager.httpOperationDict = [NSMutableDictionary dictionary];
            manager.downVideoArray = [NSMutableArray array];
            
        }
    });
    
    return manager;
}
/*经过研究，AFN 的继续和暂停下载不是线程安全的，使用会经常出错*/
#pragma mark 开始下载
-(void)startAVideoWithVideoModel:(VideoModel *)model
{
//    NSLog(@"........%@", CachesPath);
    if (!self.downLoadQueue) {
        self.downLoadQueue = [[NSOperationQueue alloc] init];
        self.downLoadQueue.maxConcurrentOperationCount = 3;
    }
    DwonLoad *ope = [[DwonLoad alloc] initWithDownLoadVideoModel:model];
    ope.updataBlock = ^(VideoModel *model){
        if (self.updata) {
            self.updata(model);
        }
    };
    
    [self.httpOperationDict setObject:ope forKey:model.urlStr];
    [self.downLoadQueue addOperation:ope];
    [self.downVideoArray addObject:model];
   
}

#pragma mark 暂停下载
-(void)downloadPausewithModel:(VideoModel *)model
{
    
    
    DwonLoad *ope = [self.httpOperationDict objectForKey:model.urlStr];
    if (!ope.isFinished) {
        [ope downLoadPause];
    }
    
}

#pragma mark 断点继续下载
-(void)downloadResumeWithModel:(VideoModel *)model
{
    DwonLoad *ope = [self.httpOperationDict objectForKey:model.urlStr];
    if (!ope.isFinished) {
        [ope downLoadResume];
    }
}




@end
